﻿/**************************************************************************
创建时间:	2020/6/28
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils.Authorizes;
using ZhCun.WebUtils.Configs;

namespace ZhCun.WebUtils
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public abstract class AuthorizeBaseAttribute : Attribute, IAuthorizationFilter
    {
        /// <summary>
        /// 是否验证，如果为false 则取消验证
        /// </summary>
        public bool IsAuth { set; get; } = true;

        protected bool CheckFilter(FilterContext context)
        {
            if (!context.IsEffectivePolicy(this)) return false;

            if (!IsAuth) return false;

            return true;
        }

        protected abstract void AuthorizationHandle(AuthorizationFilterContext context);

        public void OnAuthorization(AuthorizationFilterContext context)
        {
            //if (!context.IsEffectivePolicy(this)) return;

            //if (!IsAuth) return;

            AuthorizationHandle(context);
        }
    }
}
